package org.flown.shop.entity;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.flown.shop.DirectoryInformation;
import org.flown.shop.entity.error.ReadError;

import javax.imageio.stream.FileImageOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;


/*
* 目录结构
* goods/商店id/商品id
* goods/商店id/商品id/cover.jpg
* goods/商店id/商品id/detail.json
* goods/商店id/商品id/detail
* goods/商店id/商品id/detail/name1.jpg
* goods/商店id/商品id/detail/name2.jpg
* goods/商店id/商品id/comment.json
* goods/商店id/商品id/comment
* goods/商店id/商品id/comment/评价id
* goods/商店id/商品id/comment/评价id/content.json
* goods/商店id/商品id/comment/评价id/1.jpg
* goods/商店id/商品id/comment/评价id/2.png
* goods/商店id/商品id/comment/评价id/3.jpg
* */


/*
* 表结构
* goods:
*   id,
*   seller_id,
*   store_id,
*   name,
*   price
*
* goods_comment:
*   id,user_id,
*   goods_id,
*   store_id,
*   orders_id,
*   detail_star,
*   logistic_star,
*   attitude_star,
*   type,
*   time,
*   content
* */


/**
 * 需要配置TypeHandler
 * <typeHandlers>
 *     <typeHandler handler="org.apache.ibatis.type.EnumTypeHandler"/>
 * </typeHandlers>
 * */


@EqualsAndHashCode(callSuper = true)
@Data
public class Goods extends BaseEntity
{
    @NotNull @Min(1)
    private int sellerID;
    @NotNull @Min(1)
    private int storeID;
    @NotNull
    private String name;
    @NotNull
    private float price;
    private String detail = "[]";//用时转json
    private String[] base64Picture = null;


    public Goods(){}
    public Goods(int seller_id, int store_id, String name, float price)
    {
        this(seller_id, store_id, name, price, "[]");
    }
    public Goods(int seller_id, int store_id, String name, float price, String detail)
    {
        this.sellerID = seller_id;
        this.storeID = store_id;
        this.name = name;
        this.price = price;
        this.detail = detail;
    }



    public final String getDirectoryRealPath() {return DirectoryInformation.GOODS.getRealPath() + "/" + this.storeID + "/" + this.id;}
    public final String getCoverFileRealPath() {return this.getDirectoryRealPath() + "/cover.jpg";}
    public final String getDetailFileRealPath() {return this.getDirectoryRealPath() + "/detail.json";}
    public final String getDetailDirectoryRealPath() {return this.getDirectoryRealPath() + "/detail";}


    public final String getCommentFileRealPath() {return this.getDirectoryRealPath() + "/comment.json";}
    public final String getCommentDirectoryRealPath() {return this.getDirectoryRealPath() + "/comment";}


    public String getPictureRealPath(String name, String format) {return this.getDetailDirectoryRealPath() + "/" + name + "." + format;}
    public final boolean savePictures()
            throws IOException
    {
        if (this.base64Picture == null)
            return false;
        //创建文件夹
        File directory = new File( this.getDetailDirectoryRealPath());
        if(!directory.exists())
            directory.mkdir();

        int index_picture = 0;
        ObjectMapper mapper = new ObjectMapper();
        ArrayNode root = (ArrayNode)mapper.readTree(this.detail);
        for (int i = 0; i < root.size(); i++)
        {
            JsonNode node = root.get(i);
            if (node.isObject())
            {
                File file = new File(this.getPictureRealPath(node.get("name").toString(), node.get("format").toString()));
                FileImageOutputStream output = new FileImageOutputStream(file);
                byte[] bytes = Base64.getDecoder().decode(this.base64Picture[index_picture]);
                output.write(bytes);
                output.close();
                index_picture++;
            }
        }
        return true;
    }


}



















